# coding=utf-8

import requests,os,sys,django
from bs4 import BeautifulSoup
from django.core.wsgi import get_wsgi_application
sys.path.extend([r'E:/myself/django_rest_env/moviesapi',])
os.environ.setdefault("DJANGO_SETTINGS_MODULE","moviesapi.settings")
application = get_wsgi_application()
django.setup()
from movies.models import Movie
import time,random


user_agents = ['Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1','Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50','Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11']
headers = {
    'User-Agent': random.choice(user_agents),
}
proxies = {'http':'http://10.10.10.10:8765', 'https':'https://10.10.10.10:8765'}

def insert_data():
    url = 'http://maoyan.com/films?showType=3'
    wbdata = requests.get(url, proxies=proxies, headers=headers).content
    soup = BeautifulSoup(wbdata, 'html.parser')
    print(soup)
    movie_list = soup.select('dd div a')
    for movie in movie_list:
        print(movie.get('href'))
        m_url = movie.get('href')
        m_data = requests.get(m_url, proxies=proxies, headers=headers).content
        # with open(r"C:\Users\Administrator\Desktop\送你一朵小红花_购票_剧情介绍_演职人员_图集_猫眼电影.html", "rb") as m_data:
        m_soup = BeautifulSoup(m_data, 'html.parser')
        title = m_soup.select_one("div.movie-brief-container > h1.name").get_text()
        cate = m_soup.select("a.text-link")[0].get_text()
        date = m_soup.select("div.movie-brief-container > ul > li")[2].get_text()[0:10]
        img = m_soup.select_one('div.avatar-shadow > img').get('src')
        print(title, cate, date, img)
        insertdata = Movie.objects.get_or_create(
            name = title,
            movie_cate = cate,
            movie_img = img,
            release_date = date
        )
        time.sleep(3)

if __name__ == '__main__':
    insert_data()